package example.com.terry.servlet;

import example.com.terry.utils.CookieUtils;
import example.com.terry.utils.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ClassName: QueryWorkServlet <br/>
 * Description: 记账增加接口 <br/>
 * Date: 2020/5/21 下午5:27 <br/>
 * <br/>
 *
 * @author ls
 * <p/>
 * 修改记录
 * @version 1.0<br />
 */
@WebServlet("/insertPropertyServlet")
public class InsertPropertyServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (CookieUtils.isSign(req)) {
            String cause = req.getParameter("cause");
            String id = req.getParameter("id");
            String price = req.getParameter("price");
            String status = req.getParameter("status");

            if (id != null) {
                String sql = "update tb_property set cause='" + cause + "', price = '" + price + "' , status = '" + status + "' where id=" + id;

                boolean init = JDBCUtils.init(sql);
                if (init) {
                    resp.getWriter().println("修改成功");
                } else {
                    resp.getWriter().println("修改失败");
                }
            } else {

                String sql = "insert into tb_property(cause,price,create_time,update_user,status) values('" + cause + "','" + price + "',now(),'" + CookieUtils.getUser(req) + "','" + status + "')";

                boolean init = JDBCUtils.init(sql);
                if (init) {
                    resp.getWriter().println("增加帐目记录成功");
                } else {
                    resp.getWriter().println("增加帐目记录失败");
                }
            }
        } else {
            resp.getWriter().println("非登陆状态-禁止操作");
            req.getRequestDispatcher("/login.jsp").forward(req, resp);
        }


    }
}
